from tkinter import *
from tkinter import ttk, messagebox, filedialog
from db_operations import DBOperations
import os

class DiscardUI:
    def __init__(self, root):
        self.root = root
        self.db = DBOperations()
        
        self.frame = Frame(self.root, padx=20, pady=20)
        self.frame.pack(expand=True)
        
        # 标签页
        self.notebook = ttk.Notebook(self.frame)
        self.notebook.pack(fill=BOTH, expand=True)
        
        # 单一设备销库
        self.single_tab = Frame(self.notebook)
        self.notebook.add(self.single_tab, text="单一设备销库")
        self.create_single_tab()
        
        # 批量销库
        self.batch_tab = Frame(self.notebook)
        self.notebook.add(self.batch_tab, text="批量销库")
        self.create_batch_tab()
    
    def create_single_tab(self):
        Label(self.single_tab, text="SN码:").grid(row=0, column=0, sticky=W, pady=5)
        self.single_sn_entry = Entry(self.single_tab)
        self.single_sn_entry.grid(row=0, column=1, pady=5)
        Button(self.single_tab, text="搜索", command=self.search_single).grid(row=0, column=2, padx=5)
        
        # 设备信息显示框
        self.single_info_frame = Frame(self.single_tab)
        self.single_info_frame.grid(row=1, column=0, columnspan=3, pady=10)
        
        # 设备详细信息标签
        Label(self.single_info_frame, text="设备信息:").grid(row=0, column=0, sticky=W)
        self.single_info_text = Text(self.single_info_frame, height=5, width=40)
        self.single_info_text.grid(row=1, column=0, columnspan=3)
        
        Button(self.single_tab, text="确认销库", command=self.confirm_single_discard).grid(row=2, column=1, pady=20)
    
    def create_batch_tab(self):
        Label(self.batch_tab, text="选择清单文件:").grid(row=0, column=0, sticky=W, pady=5)
        self.batch_file_entry = Entry(self.batch_tab)
        self.batch_file_entry.grid(row=0, column=1, pady=5)
        Button(self.batch_tab, text="浏览", command=self.browse_file).grid(row=0, column=2, padx=5)
        
        # 批量销库信息显示框
        self.batch_info_frame = Frame(self.batch_tab)
        self.batch_info_frame.grid(row=1, column=0, columnspan=3, pady=10)
        
        # 批量销库详细信息标签
        Label(self.batch_info_frame, text="批量销库设备:").grid(row=0, column=0, sticky=W)
        self.batch_info_text = Text(self.batch_info_frame, height=10, width=40)
        self.batch_info_text.grid(row=1, column=0, columnspan=3)
        
        Button(self.batch_tab, text="批量销库", command=self.confirm_batch_discard).grid(row=2, column=1, pady=20)
    
    def search_single(self):
        sn = self.single_sn_entry.get()
        if not sn:
            messagebox.showerror("错误", "请输入SN码")
            return
            
        equipment = self.db.get_equipment(sn)
        if equipment:
            # 清空并显示设备详细信息
            self.single_info_text.delete(1.0, END)
            self.single_info_text.insert(END, f"SN码: {equipment[0][2]}\n")
            self.single_info_text.insert(END, f"型号: {equipment[0][3]}\n")
            self.single_info_text.insert(END, f"厂商: {equipment[0][4]}\n")
            self.single_info_text.insert(END, f"类型: {equipment[0][6]}\n")
            self.single_info_text.insert(END, f"状态: {equipment[0][7]}\n")
        else:
            self.single_info_text.delete(1.0, END)
            self.single_info_text.insert(END, "未找到该设备")
    
    def confirm_single_discard(self):
        sn = self.single_sn_entry.get()
        if not sn:
            messagebox.showerror("错误", "请输入SN码")
            return
            
        if self.db.discard_equipment(sn):
            messagebox.showinfo("成功", "设备销库成功")
            self.single_sn_entry.delete(0, END)
            self.single_info_label.config(text="")
        else:
            messagebox.showerror("错误", "设备不存在或已销库")
    
    def browse_file(self):
        filepath = filedialog.askopenfilename(filetypes=[("文本文件", "*.txt")])
        if filepath:
            self.batch_file_entry.delete(0, END)
            self.batch_file_entry.insert(0, filepath)
            
            # 读取txt文件中的SN码
            try:
                with open(filepath, 'r') as f:
                    sn_list = [line.strip() for line in f if line.strip()]
                    
                # 显示SN码列表
                self.batch_info_text.delete(1.0, END)
                self.batch_info_text.insert(END, f"已选择文件: {os.path.basename(filepath)}\n")
                self.batch_info_text.insert(END, "SN码列表:\n")
                for sn in sn_list:
                    self.batch_info_text.insert(END, f"{sn}\n")
            except Exception as e:
                messagebox.showerror("错误", f"读取文件失败: {str(e)}")
    
    def confirm_batch_discard(self):
        filepath = self.batch_file_entry.get()
        if not filepath:
            messagebox.showerror("错误", "请选择清单文件")
            return
            
        # 模拟从Excel读取SN列表
        sn_list = ["SN123456789", "SN987654321"]  # 实际应用中应从Excel读取
        count = self.db.batch_discard(sn_list)
        messagebox.showinfo("成功", f"成功销库{count}台设备")
        self.batch_file_entry.delete(0, END)
        self.batch_info_label.config(text="")